﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraCharts;
using DevExpress.Utils;
using TierService;
using OrderMgr.SysSetting;
using System.Data.SqlClient;
using TierApplication;


namespace OrderMgr.OrderInfo
{
    public partial class frmSaleInvoiceDetail : TierService.frmTemplate
    {
        #region "Member Variables"
        public string sFuncID;
        public TierService.Utility.OperateMode OpMode;
        public string sUserID;
        public string _InvoiceID = "";
        public string _OrderID = "";
        public Utility.OrderType _OrderType;
        public Utility.OrderStatus _OrderStatus;

        T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
        #endregion

        public frmSaleInvoiceDetail()
        {
            InitializeComponent();
        }

        private void frmBuyToStockDetail_Load(object sender, EventArgs e)
        {
            initForm();
            if (_OrderID != "")
                txtOrderID.Text = _OrderID;
            BindSaleShipByOrderID(txtOrderID.Text);

            //switch (OpMode)
            //{
            //    case Utility.OperateMode.Query:
            //        btnSave.Visible = false;
            //        txtOrderID.BackColor = Color.Silver;
            //        txtOrderID.Properties.ReadOnly = true;
            //        showInfo(_InvoiceID);
            //        break;
            //    case Utility.OperateMode.Insert:
            //        if (_OrderID != "")
            //            txtOrderID.Text = _OrderID;
            //        BindSaleShipByOrderID(txtOrderID.Text);

            //        break;
            //    case Utility.OperateMode.Update:
            //        btnSave.Enabled = true;
            //        txtOrderID.BackColor = Color.Silver;
            //        txtOrderID.Properties.ReadOnly = true;
            //        showInfo(_InvoiceID);
            //        break;
            //    case Utility.OperateMode.Delete:
            //        break;
            //    default:
            //        break;
            //}

        }

        private void initForm()
        {

            Utility.DataToLookupComboxEx(txtBuyUserID, (new P_User()).GetList(""), "UserID", "UserName");


        }


        private void showInfo(string sID)
        {


            //*****************Order Detail*****************//
            BindOrderDetail();

        }
        private void GetFormReturValue(string str)
        {
            string sID = str;
            if (sID != "")
            {
                showInfo(sID);
            }
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            //Close();
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }



        private void btnProductAdd_Click(object sender, EventArgs e)
        {

        }


        private void BindOrderDetail()
        {

            if (_InvoiceID == "") return;

            BindSaleShipByOrderID(txtOrderID.Text);

        }


        private void txtPOrderID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmSaleInvoiceNeed fDetail = new frmSaleInvoiceNeed();
            fDetail.gFuncType = Utility.FuncType.Select;
            fDetail._IsStock = "Y";
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                txtOrderID.Text = fDetail._OrderID;
                string sPOrderID = txtOrderID.Text;
                //生成商品明细
                BindSaleShipByOrderID(sPOrderID);
            }
        }


        private void BindSaleShipByOrderID(string sOrderID)
        {
            DataTable dtOrderDetail = T_Order.GetSaleShipByOrderID(sOrderID).Tables[0];
            gcSaleProduct.DataSource = dtOrderDetail;

            BindOrderInvoiceInfo(txtOrderID.Text);
        }

        private void BindOrderInvoiceInfo(string sOrderID)
        {
            T_OrderInvoice mOrderInvoice = new T_OrderInvoice();
            DataTable dtOrderInvoiceInfoDetail = mOrderInvoice.GetSaleInvoiceInfoByOrderID(sOrderID).Tables[0];
            gcSaleInvoice.DataSource = dtOrderInvoiceInfoDetail;
        }


        private void btnAudit_Click(object sender, EventArgs e)
        {

        }

        private void btnOrderPrint_Click(object sender, EventArgs e)
        {

        }

        private void btnSubmitAudit_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否确认此单据开票信息，确认后将不能更改？", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            T_Order mOrder = new T_Order();
            //mOrder.Audit(txtInvoiceID.Text, Utility.gUserID,0);
            Close();
        }

        private void btnReturnFee_Click(object sender, EventArgs e)
        {
            if (gvSaleInvoice.RowCount < 1) return;
            DataRow dr = gvSaleInvoice.GetDataRow(gvSaleInvoice.FocusedRowHandle);
            string sID = dr["InvoiceID"].ToString();

            frmSaleInvoiceReturnFee fDetail = new frmSaleInvoiceReturnFee();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._InvoiceID = sID;
            fDetail._OrderType = Utility.OrderType.SaleReturnFee;
            fDetail.ShowDialog();

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmSaleInvoiceDetailInfo fDetail = new frmSaleInvoiceDetailInfo();
            fDetail.OpMode = TierService.Utility.OperateMode.Insert;
            fDetail._OrderID = txtOrderID.Text;
            fDetail._OrderStatus = Utility.OrderStatus.S0Input;
            fDetail._OrderType = Utility.OrderType.SaleInvoice;
            fDetail.ShowDialog();
            BindOrderInvoiceInfo(txtOrderID.Text);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvSaleInvoice.RowCount < 1) return;
            DataRow dr = gvSaleInvoice.GetDataRow(gvSaleInvoice.FocusedRowHandle);
            string sID = dr["InvoiceID"].ToString();

            frmSaleInvoiceDetailInfo fDetail = new frmSaleInvoiceDetailInfo();
            fDetail.OpMode = Utility.OperateMode.Update;
            fDetail._InvoiceID = sID;
            fDetail.ShowDialog();

            BindOrderInvoiceInfo(txtOrderID.Text);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvSaleInvoice.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此订单记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvSaleInvoice.GetDataRow(gvSaleInvoice.FocusedRowHandle);
            string sID = dr["InvoiceID"].ToString();

            T_OrderInvoice mOrder = new T_OrderInvoice();
            mOrder.Delete(sID);

            BindOrderInvoiceInfo(txtOrderID.Text);
        }

        private void txtOrderID_EditValueChanged(object sender, EventArgs e)
        {
            string sPOrderID = txtOrderID.Text;
            T_Order mSaleOrder = new T_Order();
            mSaleOrder.GetModel(sPOrderID);
            txtBuyUserID.EditValue = mSaleOrder.OrderUserID;
            //txtDepotID.EditValue = mSaleOrder.DepotID;
            if (mSaleOrder.OrderDate != null)
                txtBuyDate.DateTime = mSaleOrder.OrderDate.Value;
            txtSaleInvoiceMoney.Value = mSaleOrder.PayFee.Value;
            T_Customer mCustomer = new T_Customer();
            mCustomer.GetModel(mSaleOrder.CustID);
            txtCustName.Text = mCustomer.CustName;
            //txtDepotID.EditValue = mSaleOrder.DepotID;
        }

    }
}